import 'package:flutter/material.dart';
import 'package:shopping_group/modle/main/entity/commo_spec4app_record_entity.dart';
import 'package:shopping_group/modle/main/ui/details_to_paly_page.dart';
import 'package:shopping_group/modle/more/bloc/more_fgood_play_bloc.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/route_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';




class MoreGoodPage extends StatefulWidget {
  final String custGroupSpecRelaId;
  final String selectId;
  MoreGoodPage({Key key, this.custGroupSpecRelaId, this.selectId})
      : super(key: key);
  @override
  _MoreGoodPageState createState() => _MoreGoodPageState();
}

class _MoreGoodPageState extends State<MoreGoodPage> {
  List<CommoSpec4AppRecordEntity> _listEntity = [];
  MoreFuGoodPlayBloc _bloc;
  @override
  void initState() {
    super.initState();
    _initSync();
  }

  void _initSync() {
    _bloc = MoreFuGoodPlayBloc();
    Map<String, dynamic> data = {};
    data['val'] = this.widget.custGroupSpecRelaId;
    _bloc.initDate.add(data);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '复购商品'),
      body: Container(
        height: double.infinity,
        decoration: new BoxDecoration(
          color: Colours.title_white,
        ),
        child: Column(
          children: <Widget>[
            // _titleView(),
            // _buildNotice(),
            Expanded(
              child: _gridView(),
            ),
          ],
        ),
      ),
    );
  }

  // Widget _titleView() {
  //   return Container(
  //     width: double.infinity,
  //     padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
  //     decoration: new BoxDecoration(
  //       color: Colours.main_01,
  //     ),
  //     child: WidgetUtils.getTextColorView(
  //         '设备状态', Colors.white, Dimens.FONT_SIZE_28),
  //   );
  // }

  Widget _gridView() {
    return StreamBuilder(
      stream: _bloc.sendData,
      initialData: _listEntity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is List) {
          _listEntity = object;
        }

        return Container(
          // margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),

          // margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
          child: GridView.builder(
              itemCount: _listEntity.length,

              //SliverGridDelegateWithFixedCrossAxisCount 构建一个横轴固定数量Widget
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  //横轴元素个数
                  crossAxisCount: 2,
                  //纵轴间距
                  mainAxisSpacing: 4.0,
                  //横轴间距
                  crossAxisSpacing: 16.0,

                  //子组件宽高长度比例
                  childAspectRatio: 0.75),
              itemBuilder: (BuildContext context, int index) {
                //Widget Function(BuildContext context, int index)

                return getItemContainer(_listEntity[index]);
              }),
        );
      },
    );
  }

  Widget getItemContainer(CommoSpec4AppRecordEntity entity) {
    return GestureDetector(
      onTap: (() {
        RouteUtil.goParameter(DetailsToPalyPage(
          entity: entity,
        ));
      }),
      child: Container(
        margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 5),
        child: Material(
          borderRadius: BorderRadius.circular(4.0),
          color: Colours.title_white,
          // elevation: 5.0,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                height: 110.0,
                width: 110.0,
                margin: const EdgeInsets.all(5.0),
                child: FadeInImage.assetNetwork(
                  image: entity.imgUrl,
                  placeholder: 'images/picture_loading.png' /* 指定gif */,
                  fit: BoxFit.contain,
                ),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    margin: const EdgeInsets.only(left: 12, top: 12),
                    child: WidgetUtils.getTextMaxLinesView(
                        entity.name, Colours.text_2222, Dimens.FONT_SIZE_14,
                        overflow: TextOverflow.ellipsis),
                  ),
                  SizedBox(
                    height: 4,
                  ),
                  Row(
                    children: <Widget>[
                      Container(
                        margin: const EdgeInsets.only(left: 12, bottom: 10),
                        child: WidgetUtils.getTextColorView(
                            '价格:${entity.outPrice}',
                            Colours.text_2222,
                            Dimens.FONT_SIZE_16,
                            textAlign: TextAlign.start,
                            fontweight: FontWeight.w500),
                      ),
                      SizedBox(
                        width: 4,
                      ),
                      Container(
                        margin: const EdgeInsets.only(left: 12, bottom: 10),
                        child: WidgetUtils.getTextColorView(
                            '数量:${entity.hadShopCt}',
                            Colours.main_01,
                            Dimens.FONT_SIZE_14,
                            textAlign: TextAlign.start),
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
